using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FanDongping.Stock
{
    public partial class FmEditTrade : Form
    {
        FormStatus status = FormStatus.Add;

        Trade trade;

        public FmEditTrade()
        {
            InitializeComponent();
            CreateNewTrade();

        }

        public FmEditTrade(Trade trade)
        {
            InitializeComponent();
            this.trade = trade;
            status = FormStatus.Edit;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (HaveError() == false)
            {

                if (status == FormStatus.Add)
                {
                    StockData.TradeHistory.Add(trade);
                }
                CreateNewTrade();
                BindingTrade();
            }
        }

        private bool HaveError()
        {
            bool haveError = false;

            if (trade.Stock == null)
            {
                ep.SetError(txtStock, "Error,Stock not allow empty!");
                haveError = true;
            }
            else
            {
                ep.SetError(txtStock, null);
            }

            return haveError;


        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            //TradeType tt = DataUtils.ParseTradeType(combTradeType.SelectedText);
            //if (tt == TradeType.SELL)
            //{
            //    // double d= numCount.Value*txtPrice
            //}
        }

        private void FmAddRecord_Load(object sender, EventArgs e)
        {
            radioBuy.Text = TradeType.BUY.ToString();
            radioSell.Text = TradeType.SELL.ToString();
            BindingTrade();
        }

        private void CreateNewTrade()
        {
            trade = new Trade();
            trade.OtherFee = (double)trade.CalcOtherFee;
        }

        private void BindingTrade()
        {
            bindTrade.DataSource = trade;
            if (trade.Stock != null)
            {
                txtStock.Text = trade.Stock.ID;
            }
            bindingSourceStocks.DataSource = StockData.StockList.Stocks;
            bindTradeType(trade.TradeType);
        }

        private void bindTradeType(TradeType tt)
        {
            if (tt == TradeType.BUY)
            {
                radioBuy.Checked = true;
            }
            else
            {
                radioSell.Checked = true;
            }
        }

        private void txtPrice_Validated(object sender, EventArgs e)
        {
            //labTotalMoney.Text = t.CalculateTurnover.ToString();

            txtTradeTax.Text = trade.CalcStampTax.ToString();
            txtTradeFee.Text = trade.CalcTradeFee.ToString();
            bindTrade.EndEdit();

            labTurnOver.Text = trade.CalcTurnover.ToString();
            txtRetainedMoney.Text = trade.CalcRetainedMoney.ToString();
        }

        private void numCount_Validated(object sender, EventArgs e)
        {
            //Trade tmp = bindTrade.Current as Trade;
            //tmp.StampTax = tmp.CalcStampTax;
            //tmp.TradeFee = tmp.CalcTradeFee;
            //tmp.RetainedMoney = tmp.CalcRetainedMoney;
            //labTurnOver.Text = t.CalcTurnover.ToString();

            txtTradeTax.Text = trade.CalcStampTax.ToString();
            txtTradeFee.Text = trade.CalcTradeFee.ToString();
            bindTrade.EndEdit();

            labTurnOver.Text = trade.CalcTurnover.ToString();
            txtRetainedMoney.Text = trade.CalcRetainedMoney.ToString();
        }

        private void numCount_ValueChanged(object sender, EventArgs e)
        {
            // txtRetainedMoney.Text = t.CalcRetainedMoney.ToString();
            //labTurnOver.Text = t.CalcTurnover.ToString();
            //txtRetainedMoney.Text = t.CalcRetainedMoney.ToString();
        }

        private void txtTradeTax_Validated(object sender, EventArgs e)
        {
            txtRetainedMoney.Text = trade.CalcRetainedMoney.ToString();
        }

        private void txtTradeFee_Validated(object sender, EventArgs e)
        {
            txtRetainedMoney.Text = trade.CalcRetainedMoney.ToString();
        }

        private void txtOtherFee_Validated(object sender, EventArgs e)
        {
            txtRetainedMoney.Text = trade.CalcRetainedMoney.ToString();
        }

        private void txtRetainedMoney_Validated(object sender, EventArgs e)
        {

        }

        private void labTotalMoney_Click(object sender, EventArgs e)
        {

        }

        private void labTotalMoney_TextChanged(object sender, EventArgs e)
        {

        }

        private void combTradeType_Validated(object sender, EventArgs e)
        {
            //setSign();
            txtRetainedMoney.Text = trade.CalcRetainedMoney.ToString();
        }

        private void combTradeType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void radioSell_CheckedChanged(object sender, EventArgs e)
        {
            if (radioBuy.Checked)
            {
                grpType.BackColor = Color.GreenYellow;
                trade.TradeType = TradeType.BUY;
            }
            else
            {
                grpType.BackColor = Color.Red;
                trade.TradeType = TradeType.SELL;
            }
            labTurnOver.Text = trade.CalcTurnover.ToString();
            txtRetainedMoney.Text = trade.CalcRetainedMoney.ToString();
        }

        private void combStock_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        private void txtStock_Enter(object sender, EventArgs e)
        {
            lstStock.Visible = true;
        }

        private void txtStock_Leave(object sender, EventArgs e)
        {
            if (lstStock.Focused == false)
            {
                lstStock.Visible = false;
            }
        }

        private void lstStock_Leave(object sender, EventArgs e)
        {
            if (txtStock.Focused == false)
            {
                lstStock.Visible = false;
            }
        }

        private void lstStock_DoubleClick(object sender, EventArgs e)
        {
            trade.Stock = StockData.StockList.GetStock(lstStock.SelectedValue as string);
            this.txtStock.Text = trade.Stock.ID;
            txtStock.Focus();
            this.lstStock.Visible = false;
            HaveError();

        }

        private void txtStock_Validated(object sender, EventArgs e)
        {
            this.trade.Stock = StockData.StockList.GetStock(this.txtStock.Text);
            HaveError();
        }

    }
}